package cn.wizzer.app.web.commons.ext.websocket.action.msg.attendant;

import cn.wizzer.app.sys.modules.services.SysSensitiveService;
import cn.wizzer.app.user.modules.models.UserChatMsg;
import cn.wizzer.app.user.modules.models.enums.MsgTypeEnum;
import cn.wizzer.app.user.modules.models.vo.ChatUserVO;
import cn.wizzer.app.user.modules.services.UserChatMsgService;
import cn.wizzer.app.web.commons.ext.websocket.filter.AttendantFilter;
import cn.wizzer.app.web.commons.ext.websocket.filter.TokenFilter;
import cn.wizzer.app.web.commons.ext.websocket.main.action.BaseNettyAction;
import cn.wizzer.app.web.commons.ext.websocket.main.action.NettyAction;
import cn.wizzer.app.web.commons.ext.websocket.main.base.NettyCache;
import cn.wizzer.app.web.commons.ext.websocket.main.result.ChatResult;
import cn.wizzer.app.web.commons.utils.NettyMsgUtil;
import cn.wizzer.framework.base.Result;
import cn.wizzer.framework.util.JSONUtil;
import cn.wizzer.framework.util.param.Param;
import cn.wizzer.framework.util.param.Param4Map;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import io.netty.channel.Channel;
import org.apache.commons.lang3.EnumUtils;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

/**
 * 客服回信
 */
@IocBean
@NettyAction(uri = "/attendant/attendant/send", filters = {TokenFilter.class, AttendantFilter.class})
public class AttendantSendAction extends BaseNettyAction {

    @Inject
    @Reference
    private UserChatMsgService chatMsgService;
    @Inject
    @Reference
    private SysSensitiveService sensitiveService;

    @Override
    public Result action(Channel income,
                         @Param4Map(fields = {@Param(name = "target", required = true),
                                 @Param(name = "content", required = true),
                                 @Param(name = "type", required = true)}) JSONObject params) {
        ChatUserVO from = NettyCache.globalGet(params.get("appid"));
        String userId = params.getString("target");
        ChatUserVO target = NettyCache.globalGet(userId);
        if (!from.getUsers().contains(userId)) {
            return Result.error("该用户已离开");
        }
        UserChatMsg msg = new UserChatMsg();
        //过滤敏感词
        msg.setContent(sensitiveService.filter(params.getString("content")));
        msg.setCtAt(System.currentTimeMillis());
        msg.setToUser(userId);
        msg.setUserId(from.getId());
        msg.setType(EnumUtils.getEnum(MsgTypeEnum.class, params.get("type").toString()));

        msg = chatMsgService.insert(msg);
        if (target != null) {
            //发给用户
            NettyMsgUtil.send(target.getChannel(), ChatResult.MessageTypeEnum.ATTENDANT, ChatResult.ActionEnum.SEND, from, JSONUtil.toJSONString(msg));
            //该用户对应的客服，若没有客服则将用户的对应客服设置为自己
            String targetAttendantId = NettyCache.localGet(target.getChannel(), "targetAttendant");
            if(targetAttendantId == null){
                //缓存指定客服
                NettyCache.localSet(target.getChannel(),"targetAttendant", from.getId());
            }
        }
        //发给自己
        NettyMsgUtil.send(income, ChatResult.MessageTypeEnum.ATTENDANT, ChatResult.ActionEnum.SEND, from, JSONUtil.toJSONString(msg));
        return Result.success(msg);
    }

}
